package com.yihuier.yihuier.Controller.Common;

import com.yihuier.yihuier.Entity.RoomManager.Room;
import com.yihuier.yihuier.Mapper.RoomManager.CompanyMapper;
import com.yihuier.yihuier.Service.RoomManager.CompanyService;
import com.yihuier.yihuier.Service.RoomManager.RoomService;
import com.yihuier.yihuier.Util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 此类由于RoomController创建失败，故在此共用一个控制器
 * */
@RestController
@RequestMapping("/Common/")
public class CommomController {

    @Autowired
    private RoomService roomService;

    @Autowired
    private CompanyService companyService;

    @PostMapping("MeetingRoomPic.json")
    public ResponseMessage getMeetingRoomPicUrl(@PathParam("room_id") int room_id, HttpServletRequest request) {
        String MeetingRoomPicUrl = roomService.getMeetingRoomPicUrl(room_id);
        HashMap<String, String> data = new HashMap<>();
        data.put("room_pic_url", FileController.filePathDispose(MeetingRoomPicUrl));
        return new ResponseMessage(ResponseMessage.SUCCESS, "会议室图片获取成功", data);
    }

    /**
     * 轮播图广告
     */
    @GetMapping("CarouselAdvertisements.json")
    public ResponseMessage getCarouselAdvertisements(HttpServletRequest request) {
        Date day = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String presentTime = df.format(day);//获取系统当前时间
        List<Room> CarouselAdvertisementsRooms = roomService.getCarouselAdvertisementsRooms();
        ArrayList<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
        for (Room room : CarouselAdvertisementsRooms) {
            LinkedHashMap<String, Object> roomMap = new LinkedHashMap<>();
            Map<String, Object> roomToCompanyTimeAndName = companyService.getRoomToCompanyTimeAndName(room.getRoom_belong_company());
            if (roomToCompanyTimeAndName == null) {
                continue;
            }
            Date company_deadline_date = new Date();
            Date presentTime_date = new Date();
            try {
                company_deadline_date = (Date) roomToCompanyTimeAndName.get("company_deadline");//公司服务截止时间
                presentTime_date = df.parse(df.format(day));//当前时间
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (company_deadline_date.before(presentTime_date)) {//如果母公司停止服务
                continue;
            }
            roomMap.put("room_id", room.getRoom_id());
            roomMap.put("room_name", room.getRoom_name());
            roomMap.put("room_capacity", room.getRoom_capacity());
            roomMap.put("room_belongs_company_name", roomToCompanyTimeAndName.get("company_name"));
            roomMap.put("room_pic_url", FileController.filePathDispose(room.getRoom_pic_url()));
            data.add(roomMap);
        }
        return new ResponseMessage(ResponseMessage.SUCCESS, "轮播图广告获取成功", data);
    }

}
